package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Evaluation;
import ex.com.jm.food.service.CompanyEvalService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class CompanyEvalAction extends ActionSupport {

	
	private static final long serialVersionUID = 1L;
	
	private CompanyEvalService companyEvalService;
	private Evaluation evaluation;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String comId;
	private String menuName;
	private Integer start;
	private String s_date;
	private String e_date;
	private String order;
	private String sort;
	private Map<String, String> dateMap;
	public CompanyEvalService getCompanyEvalService() {
		return companyEvalService;
	}
	public void setCompanyEvalService(CompanyEvalService companyEvalService) {
		this.companyEvalService = companyEvalService;
	}
	public Evaluation getEvaluation() {
		return evaluation;
	}
	public void setEvaluation(Evaluation evaluation) {
		this.evaluation = evaluation;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public Integer getStart() {
		return start;
	}
	public void setStart(Integer start) {
		this.start = start;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	

	public String evalList(){
		dateMap=DateUtils.getDate();
		return "evalList";
	}
	
	public void getEvals(){
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8");  
		 int offset=(initPage-1)*pageRow;
		 List<Evaluation> evaluations=this.getCompanyEvalService().getEvaluations(offset, pageRow, comId, menuName, start, s_date, e_date, order, sort);
	     JSONArray joeval=JSONArray.fromObject(evaluations);
	     totalCount=this.getCompanyEvalService().evalCount(comId, menuName, start, s_date, e_date);
	     JSONObject jo=JsonUtils.buildJson(joeval, initPage, pageRow, totalCount);
	     PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(jo);
			out.print(jo);
			out.flush();
			out.close();
	}
}
